<template>
<div id="my-space">
  <p>正在跳转，请稍后..</p>
</div>
</template>
<script setup lang="ts">
// 检查是否是个人空间
import { useLoginUserStore } from '@/stores/useUserLoginStore.ts'
import { useRouter } from 'vue-router'
import { listSpaceVoByPageUsingPost } from '@/request/api/spaceController.ts'
import { message } from 'ant-design-vue'
import { onMounted } from 'vue'
import { SPACE_TYPE_ENUM } from '@/constants/spaceUserConstant.ts'
const router = useRouter()

const checkUserSpace = async () => {
  const loginUserStore = useLoginUserStore()
  if (!loginUserStore.loginUser?.id) {
    // 未登录重定向到登录也
    router.replace({
      path: '/user/login'
    })
    return
  }
  // 获取用户空间信息
  const { data:res } = await listSpaceVoByPageUsingPost({
    userId: loginUserStore.loginUser?.id,
    pageNum: 1,
    pageSize: 1,
    // 默认获取私有空间信息
    spaceType: SPACE_TYPE_ENUM.PRIVATE
  })
  if (res.code ==0) {
    if(res.data?.records&&res.data?.records?.length > 0) {
      const space=res.data?.records[0]
      router.replace(`/space/${space?.id}`)
    } else {
      router.replace(`/add-space`)
      message.warn('请先创建空间')
    }
  } else {
    message.error('获取空间信息失败' + res.message)
  }
};
onMounted(()=>{
  checkUserSpace()
})
</script>
<style scoped lang="less">

</style>
